<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends Admin_Controller {
	
	function __construct()
	{
		parent::__construct(3);
	}
	
	public function index($page = 1)
	{
		$vData = array(
			'users' => $this->admin->getUsers($page)
		);

		$this->load->library('pagination');
		$config = array(
			'base_url' => base_url().'admin/users/index',
			'total_rows' => $this->user->getUsersCount(),
			'per_page' => AdminVars::$pageSize,
			'uri_segment' => 4,
			'use_page_numbers' => true
		);
		$this->pagination->initialize($config);
		$vData['pagination'] = $this->pagination->create_links();

		$data = array(
			'content' => $this->load->view('admin/users', $vData, true)
		);
		
		$this->getPage($data);
	}
	
	public function edit($id)
	{
		$vData = array(
			'user' => $this->user->get($id),
			'socs' => $this->user->getSocsByUserId($id)
		);
		
		$data = array(
			'content' => (empty($vData['user']))?
							'Здесь таких нет':
							$this->load->view('admin/users_edit', $vData, true)
		);
		
		$this->getPage($data);
	}
	
	public function save()
	{
		if (empty($_POST['id']) || (!($id = abs($_POST['id']))))
			die();
		
		$this->db->where('id', $id);
		$user = $this->db->get('user')->row();
		
		if (empty($user)) die();
		
		if ($user->pAdmin && $this->user->id !=$id)
			die('Никто не вправе править админа');
		
		$upd = array(
			'pName' => $_POST['pName'],
			//'pAdmin' => (isset($_POST['pAdmin']) && ($_POST['pAdmin'] == '1'))?1:0
		);
		
		if (empty($_POST['pModer'][0]))
		{
			$upd['pModer'] = '';
		}
		else
		{
			unset($_POST['pModer'][0]);
			$upd['pModer'] = join(array_keys($_POST['pModer']));
		}
		
		if (!empty($_POST['pwd_1']) && !empty($_POST['pwd_2']) && ($_POST['pwd_1'] === $_POST['pwd_2']))
		{
			$upd['pHash'] = md5($user->pEmail.$_POST['pwd_1']);
		}
		
		$this->db->where('id', $id);
		$this->db->update('user', $upd);
		
		redirect(base_url().'admin/users/');
	}
}

?>